package com.example.chatchatbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chatchatbackend.entity.dto.Roboot;
import com.example.chatchatbackend.entity.vo.response.RobootVO;
import com.example.chatchatbackend.mapper.RobootMapper;
import com.example.chatchatbackend.service.RobootService;
import com.example.chatchatbackend.utils.TimeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 丁真
 * @since 2024-10-28 05:03:47
 */
@Service
public class RobootServiceImpl extends ServiceImpl<RobootMapper, Roboot> implements RobootService {

    private static final LocalDateTime currentTime  = TimeUtil.Time_Now.getCurrentTime();

    @Autowired
    private RobootMapper robootMapper;
    @Override
    public List<RobootVO> getAllRoboot() {
        List<Roboot> robootList = this.query().list();
        List<RobootVO> robootVOList = new ArrayList<>();
        for(Roboot roboot : robootList){
            robootVOList.add(new RobootVO(roboot.getId(),roboot.getName(),roboot.getAvatar(),roboot.getUserId(),roboot.getCreatedTime(),roboot.getUpdatedTime(),roboot.getUserName()));
        }
        return robootVOList;
    }

    @Override
    public RobootVO insertRoboot(RobootVO robootVO) {
        Roboot comment = new Roboot(null,robootVO.getName(),robootVO.getAvatar(),robootVO.getUserId(),this.currentTime,this.currentTime);
        if(this.save(comment))
            return robootVO;
        return null;
    }

    @Override
    public List<RobootVO> insertRobootList(List<RobootVO> robootVOList) {

        List<Roboot> robootList = new ArrayList<>();

        for (RobootVO robootVO : robootVOList) {
            robootList.add(new Roboot(null,robootVO.getName(),robootVO.getAvatar(),robootVO.getUserId(),currentTime,this.currentTime));
        }
        robootMapper.insertList(robootList);
        return robootVOList;
    }

    @Override
    public RobootVO updateRoboot(RobootVO robootVO) {
        Roboot roboot = new Roboot();
        BeanUtils.copyProperties(robootVO, roboot);
        roboot.setUpdatedTime(this.currentTime);
        robootMapper.updateRoboot(roboot);
        return robootVO;
    }

    @Override
    public List<RobootVO> findByTitle(String name) {

        LambdaQueryWrapper<Roboot> wrapper = new LambdaQueryWrapper<>();
        wrapper.likeRight(StringUtils.hasText(name), Roboot::getName, name);
        List<Roboot> robootList = robootMapper.selectList(wrapper);

        List<RobootVO> robootVOList = new ArrayList<>();

        for (Roboot roboot : robootList) {
            RobootVO robootVO = new RobootVO();
            BeanUtils.copyProperties(roboot, robootVO);
            robootVOList.add(robootVO);  // 添加到结果列表
        }
        return robootVOList;
    }

}
